{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tensorflow 2 ,加载训练好的权重\n",
    "\n",
    "数据流量化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "#from tensorflow import keras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist = tf.keras.datasets.mnist\n",
    "img_rows,img_cols = 28,28\n",
    "(x_train_, y_train_), (x_test_, y_test_) = mnist.load_data()\n",
    "x_train = x_train_.reshape(x_train_.shape[0],img_rows,img_cols,1)\n",
    "x_test = x_test_.reshape(x_test_.shape[0],img_rows,img_cols,1)\n",
    "\n",
    "x_train = x_train.astype('float32')\n",
    "x_test = x_test.astype('float32')\n",
    "x_train = x_train / 256\n",
    "x_test = x_test / 256\n",
    "y_train_onehot = tf.keras.utils.to_categorical(y_train_)\n",
    "y_test_onehot = tf.keras.utils.to_categorical(y_test_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy: 0.98500\n"
     ]
    }
   ],
   "source": [
    "model = tf.keras.Sequential()\n",
    "model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')\n",
    "score = model.evaluate(x_test, y_test_onehot, verbose=0)\n",
    "#print('Test loss:', score[0])\n",
    "print('Test accuracy:', \"{:.5f}\".format(score[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、 数据流量化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'conv2d_input', 'index': 1, 'shape': array([ 1, 28, 28,  1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]\n",
      "[{'name': 'Identity', 'index': 0, 'shape': array([ 1, 10]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]\n"
     ]
    }
   ],
   "source": [
    "interpreter= tf.lite.Interpreter(model_path=\"models/tflite_tf2_dy.tflite\")\n",
    "interpreter.allocate_tensors()\n",
    "\n",
    "input_details=interpreter.get_input_details()\n",
    "output_details=interpreter.get_output_details()\n",
    "\n",
    "print(input_details)\n",
    "print(output_details)\n",
    "y_lebal=y_test_onehot.argmax(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9844\n"
     ]
    }
   ],
   "source": [
    "# 查看量化后的准确率\n",
    "pre = []\n",
    "for i in range (len(x_test)):\n",
    "    x_test1 = x_test[i].reshape(1,28,28,1).astype(np.float32)\n",
    "    interpreter.set_tensor(input_details[0]['index'],x_test1)\n",
    "    interpreter.invoke()\n",
    "    output_data = interpreter.get_tensor(output_details[0]['index'])\n",
    "    pre.append(output_data.argmax())\n",
    "\n",
    "temp = (pre==y_lebal)\n",
    "acc=sum(temp)/10000\n",
    "print(acc)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看错误预测的位置\n",
    "wrong_list=np.where(temp==False) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "184"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wrong_list[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.00390625 0.         0.70703125 0.1171875  0.         0.\n",
      "  0.         0.         0.17578125 0.        ]]\n"
     ]
    }
   ],
   "source": [
    "# 查看错误预测的图片\n",
    "x_test1 = x_test[wrong_list[0][1]].reshape(1,28,28,1).astype(np.float32)\n",
    "interpreter.set_tensor(input_details[0]['index'],x_test1)\n",
    "interpreter.invoke()\n",
    "output_data = interpreter.get_tensor(output_details[0]['index'])\n",
    "print(output_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAOBUlEQVR4nO3db6hc9Z3H8c9376YIpmhMrhqMeJsiUQmYhiGIJsE/bIlCSPqgSwM2dyV4+0AlxRKU7IMqRpB12+KDtXC7Cb2arrWkmkSQbuVSlfggOMrd3LjRJpW7aZp/c5UYI4Su6Xcf3JPlJt7zm5s5Z+ZM8n2/YJiZ853fnK+Dn5y58zszP3N3Abj0/V3VDQDoDMIOBEHYgSAIOxAEYQeC+PtO7mzOnDne19fXyV0CoYyNjWl8fNymqhUKu5mtkPScpB5J/+7uz6Qe39fXp3q9XmSXABJqtVpureW38WbWI+nfJN0r6RZJa8zsllafD0B7FfmbfYmkA+7+sbv/VdKvJa0qpy0AZSsS9usk/XnS/UPZtnOY2YCZ1c2s3mg0CuwOQBFFwj7VhwBfOffW3Qfdvebutd7e3gK7A1BEkbAfknT9pPvzJB0u1g6AdikS9ncl3Whm3zCzr0n6nqSd5bQFoGwtT725+5dm9rCk/9TE1NsWd/+gtM4AlKrQPLu7vy7p9ZJ6AdBGnC4LBEHYgSAIOxAEYQeCIOxAEIQdCIKwA0EQdiAIwg4EQdiBIAg7EARhB4Ig7EAQhB0IgrADQRB2IAjCDgRB2IEgCDsQBGEHgiDsQBAdXbIZ7XHq1Knc2rZt29q673feeSdZ37x5c25t9erVybEDAwPJ+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IOBEHYgSAIOxAEYQeCIOxAEIQdCMLcvXM7M2tI+p9Jm+ZIGu9YAxemW3vr1r4kemtVmb3d4O5T/v5bR8P+lZ2b1d29VlkDCd3aW7f2JdFbqzrVG2/jgSAIOxBE1WEfrHj/Kd3aW7f2JdFbqzrSW6V/swPonKqP7AA6hLADQVQSdjNbYWYfmdkBM3u8ih7ymNmYmY1my1DXK+5li5kdN7O9k7ZdZWZvmNn+7HrKNfYq6q0rlvFOLDNe6WtX9fLnHf+b3cx6JP1R0j9IOiTpXUlr3P2/O9pIDjMbk1Rz98pPwDCz5ZJOSXrB3Rdm2/5F0qfu/kz2D+Usd3+sS3p7QtKpqpfxzlYrmjt5mXFJqyX9kyp87RJ9/aM68LpVcWRfIumAu3/s7n+V9GtJqyroo+u5+9uSPj1v8ypJQ9ntIU38z9JxOb11BXc/4u7vZ7c/l3R2mfFKX7tEXx1RRdivk/TnSfcPqbvWe3dJvzez98xsoOpmpnCNux+RJv7nkXR1xf2cr+ky3p103jLjXfPatbL8eVFVhH2qpaS6af7vDndfLOleSQ9lb1cxPdNaxrtTplhmvCu0uvx5UVWE/ZCk6yfdnyfpcAV9TMndD2fXxyW9qu5bivrY2RV0s+vjFffz/7ppGe+plhlXF7x2VS5/XkXY35V0o5l9w8y+Jul7knZW0MdXmNnl2QcnMrPLJX1b3bcU9U5J/dntfkk7KuzlHN2yjHfeMuOq+LWrfPlzd+/4RdJ9mvhE/k+S/rmKHnL6mi/pv7LLB1X3JuklTbyt+19NvCNaJ2m2pGFJ+7Prq7qotxcljUrao4lgza2ot6Wa+NNwj6SR7HJf1a9doq+OvG6cLgsEwRl0QBCEHQiCsANBEHYgCMIOBEHYgSAIOxDE/wF7Ry9La8vHrQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "def plot_image(image):\n",
    "    plt.imshow(image.reshape(28,28),cmap='binary')\n",
    "    plt.show()\n",
    "plot_image(x_test_[wrong_list[0][1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、查看网络的权重等信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'Identity_int8', 'index': 0, 'shape': array([ 1, 10]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.00390625, -128)}, {'name': 'conv2d_input_int8', 'index': 1, 'shape': array([ 1, 28, 28,  1]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.003921568859368563, -128)}, {'name': 'sequential/conv2d/Conv2D/ReadVariableOp', 'index': 2, 'shape': array([1, 3, 3, 8]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.0, 0)}, {'name': 'sequential/conv2d/Conv2D_bias', 'index': 3, 'shape': array([8]), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0, 0)}, {'name': 'sequential/conv2d/Relu', 'index': 4, 'shape': array([ 1, 26, 26,  8]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.01157423760741949, -128)}, {'name': 'sequential/conv2d_1/Conv2D/ReadVariableOp', 'index': 5, 'shape': array([12,  3,  3,  8]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.0, 0)}, {'name': 'sequential/conv2d_1/Conv2D_bias', 'index': 6, 'shape': array([12]), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0, 0)}, {'name': 'sequential/conv2d_1/Relu', 'index': 7, 'shape': array([ 1,  7,  7, 12]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.04062683880329132, -128)}, {'name': 'sequential/dense/MatMul', 'index': 8, 'shape': array([ 1, 10]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.19927917420864105, 51)}, {'name': 'sequential/dense/MatMul/ReadVariableOp/transpose', 'index': 9, 'shape': array([ 10, 192]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.008781291544437408, 0)}, {'name': 'sequential/dense/MatMul_bias', 'index': 10, 'shape': array([10]), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0003567561216186732, 0)}, {'name': 'sequential/max_pooling2d/MaxPool', 'index': 11, 'shape': array([1, 9, 9, 8]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.01157423760741949, -128)}, {'name': 'sequential/max_pooling2d_1/MaxPool', 'index': 12, 'shape': array([ 1,  4,  4, 12]), 'dtype': <class 'numpy.int8'>, 'quantization': (0.04062683880329132, -128)}, {'name': 'conv2d_input', 'index': 13, 'shape': array([ 1, 28, 28,  1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}, {'name': 'Identity', 'index': 14, 'shape': array([ 1, 10]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]\n"
     ]
    }
   ],
   "source": [
    "interpreter= tf.lite.Interpreter(model_path=\"models/tflite_tf2_8.tflite\")\n",
    "interpreter.allocate_tensors()\n",
    "input = interpreter.get_tensor_details()\n",
    "print(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_test1 = x_test[0].reshape(1,28,28,1).astype(np.float32)\n",
    "interpreter.set_tensor(input_details[0]['index'],x_test1)\n",
    "interpreter.invoke()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.         0.         0.         0.         0.         0.\n",
      "  0.         0.99609375 0.         0.        ]]\n"
     ]
    }
   ],
   "source": [
    "output_data = interpreter.get_tensor(14)\n",
    "print(output_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "oldHeight": 392.183666,
   "position": {
    "height": "40px",
    "left": "607px",
    "right": "20px",
    "top": "17px",
    "width": "575.333px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "varInspector_section_display": "none",
   "window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
